package com.kepler.social.domain.user.account;

import com.kepler.social.common.constants.TipsConst;
import com.kepler.social.common.utils.Safes;
import com.kepler.social.common.utils.ValidateAssert;
import com.kepler.social.user.dao.domain.UserInfo;
import com.kepler.social.user.dao.domain.UserSocialInfo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Data
@NoArgsConstructor
public class UserAccountAggregate {

    private UserInfo root;

    private UserSocialInfo socialInfo;

    UserAccountAggregate(UserInfo root) {
        this.root = root;
    }

    UserAccountAggregate(UserInfo root, UserSocialInfo socialInfo) {
        this(root);
        this.socialInfo = socialInfo;
    }

    public void checkIntegrity() {
        ValidateAssert.isTrue(Objects.isNull(root) && Objects.isNull(socialInfo)
                        || Objects.nonNull(root) && Objects.nonNull(socialInfo),
                TipsConst.system_error_data_not_integrity);
    }

    public void checkExist() {
        checkIntegrity();
        ValidateAssert.isTrue(isExist(), TipsConst.user_not_exists);
    }

    public boolean isExist() {
        checkIntegrity();
        return Objects.nonNull(root) && Objects.nonNull(socialInfo);
    }

    public Long lmt() {
        Long lmtRoot = Objects.isNull(root) ? null : root.getModifiedTime();
        Long lmtSocial = Objects.isNull(socialInfo) ? null : socialInfo.getModifiedTime();
        if (Objects.isNull(lmtRoot) && Objects.isNull(lmtSocial)) {
            return 0L;
        }
        return Safes.of(lmtRoot, 0L).compareTo(Safes.of(lmtSocial, 0L)) > 0
                ? lmtRoot
                : lmtSocial;
    }

}
